# 使用二进制模式复制文件内容，避免任何编码或行尾字符问题
import os
import re

# 文件路径
original_file = r'd:\未知\yolo_apply\video_processing\video_processor.py'
backup_file = r'd:\未知\yolo_apply\video_processing\video_processor.py.backup'

# 读取备份文件内容（二进制模式）
with open(backup_file, 'rb') as f:
    binary_content = f.read()

# 转换为字符串进行处理
try:
    content = binary_content.decode('utf-8')
except UnicodeDecodeError:
    # 尝试其他编码
    content = binary_content.decode('latin-1')

# 替换问题代码块
problem_pattern = r'# 错误率过高时触发警告和恢复\s+.*?\s+.*?\s+.*?\s+.*?\s+.*?\s+.*?\s+error_count = 0'
replacement = '''# 错误率过高时触发警告和恢复\n                current_time = time.time()\n                if error_count > 10 and (current_time - last_error_time > 30):\n                    logger.warning("处理错误率过高: " + str(self.camera_id) + ", 最近错误数: " + str(error_count))\n                    if hasattr(self, "_handle_error"):\n                        self._handle_error(Exception("30秒内出现" + str(error_count) + "个错误"), "high_error_rate")\n                    last_error_time = current_time\n                    error_count = 0'''

new_content = re.sub(problem_pattern, replacement, content, flags=re.DOTALL)

# 写回文件（二进制模式）
with open(original_file, 'wb') as f:
    f.write(new_content.encode('utf-8'))

print(f"已使用二进制模式复制并修复了 {original_file} 文件")
print("现在让我们再次尝试编译...")